package com.block.datapush.common.filter.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.block.common.service.LogFactory;
import com.block.datapush.common.enums.FilterErrorEnum;
import com.block.datapush.common.filter.IFilterHandlerService;
import com.block.datapush.common.model.param.FilterHandlerParam;
import com.block.datapush.common.model.result.FilterHandlerResult;
import com.block.db.entity.MemberExtInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.time.LocalTime;

@Service
public class FilterTimeHandlerService implements IFilterHandlerService {

    private static Logger log = LogFactory.getPushLog();

    @Override
    public FilterHandlerResult handlerFilterLogic(FilterHandlerParam handlerParam, MemberExtInfo memberExtInfo) {

        if (StrUtil.hasEmpty(handlerParam.getStartTime(), handlerParam.getEndTime())){
            return FilterHandlerResult.success();
        }

        LocalTime nowTime = LocalTime.now();
        LocalTime startTime = LocalTime.parse(handlerParam.getStartTime());
        LocalTime endTime = LocalTime.parse(handlerParam.getEndTime());

        if (startTime.isBefore(endTime)){
            if (nowTime.isAfter(startTime) && nowTime.isBefore(endTime)){
                return FilterHandlerResult.success();
            }
        }else {
            if (nowTime.isAfter(startTime) || nowTime.isBefore(endTime)){
                return FilterHandlerResult.success();
            }
        }

        String message = StrUtil.format("【startTime】{}【endTime】{}【nowTime】{}", startTime, endTime, nowTime);
        log.info("【机构条件判断】false【时间】{}", message);
        return FilterHandlerResult.error(FilterErrorEnum.TIME_ERROR, message);
    }

}
